﻿using Common;
using DataLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_GuideAdd : Form
    {
        public Form_GuideAdd()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();

        private void Form_AddGuide_Load(object sender, EventArgs e)
        {
            var departments = tse.Departments.Where(u => true).Select(u => new { u.DepartmentID, u.DepartmentName }).ToList();
            this.comDepartment.DataSource = departments;
            this.comDepartment.ValueMember = "DepartmentID";
            this.comDepartment.DisplayMember = "DepartmentName";
        }
                
        private void btnSave_Click(object sender, EventArgs e)
        {            
            if (string.IsNullOrEmpty(txtGuideCard.Text))
            {
                MessageBox.Show("导游卡号不可为空");
                return;
            }
            else if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("导游姓名不可为空");
                return;
            }
            if (pm.IsTelephone(this.txtPhone.Text))
            {
                if (pm.IsIDNumber(this.txtIdCard.Text))
                {
                    var guides = tse.Guides.Where(u => u.GuideCard == this.txtGuideCard.Text).Count();
                    if (guides == 0)
                    {
                        var guide = new Guides();
                        guide.DepartmentID = Convert.ToInt32(comDepartment.SelectedValue);
                        guide.Name = string.IsNullOrEmpty(txtName.Text) ? "" : txtName.Text;
                        guide.Sex = comSex.SelectedItem.ToString();
                        guide.NickName = string.IsNullOrEmpty(txtNickName.Text) ? "" : txtNickName.Text;
                        guide.IDCard = string.IsNullOrEmpty(txtIdCard.Text) ? "" : txtIdCard.Text;
                        guide.Phone = string.IsNullOrEmpty(txtPhone.Text) ? "" : txtPhone.Text;
                        guide.Picture = "";
                        guide.GuideCard = string.IsNullOrEmpty(txtGuideCard.Text) ? "" : txtGuideCard.Text;
                        //tse.Configuration.ValidateOnSaveEnabled = false;
                        tse.Guides.Add(guide);
                        var result = tse.SaveChanges();
                        if (result >= 1)
                        {
                            this.Close();
                            MessageBox.Show("添加成功");
                        }
                        else
                        {
                            MessageBox.Show("添加失败了");
                        }
                    }
                    else
                    {
                        MessageBox.Show("该导游已存在");
                    }
                }
                else
                {
                    MessageBox.Show("身份证号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtGuideCard.Text))
            {
                MessageBox.Show("导游卡号不可为空");
                return;
            }
            else if (string.IsNullOrEmpty(txtName.Text))
            {
                MessageBox.Show("导游姓名不可为空");
                return;
            }
            if (pm.IsTelephone(this.txtPhone.Text))
            {
                if (pm.IsIDNumber(this.txtIdCard.Text))
                {
                    var guides = tse.Guides.Where(u => u.GuideCard == this.txtGuideCard.Text).Count();
                    if (guides == 0)
                    {
                        Guides guide = new Guides();
                        guide.DepartmentID = Convert.ToInt32(this.comDepartment.SelectedValue);
                        guide.Name = string.IsNullOrEmpty(this.txtName.Text) ? "" : this.txtName.Text;
                        guide.Sex = this.comSex.SelectedItem.ToString();
                        guide.NickName = string.IsNullOrEmpty(this.txtNickName.Text) ? "" : this.txtNickName.Text;
                        guide.IDCard = string.IsNullOrEmpty(this.txtIdCard.Text) ? "" : this.txtIdCard.Text;
                        guide.Phone = string.IsNullOrEmpty(this.txtPhone.Text) ? "" : this.txtPhone.Text;
                        guide.GuideCard = string.IsNullOrEmpty(this.txtGuideCard.Text) ? "" : this.txtGuideCard.Text;
                        tse.Guides.Add(guide);
                        tse.Configuration.ValidateOnSaveEnabled = false;
                        var result = tse.SaveChanges();
                        if (result >= 1)
                        {
                            this.txtName.Text = null;
                            this.txtNickName.Text = null;
                            this.txtIdCard.Text = null;
                            this.txtPhone.Text = null;
                            this.txtGuideCard.Text = null;

                        }
                    }
                    else
                    {
                        MessageBox.Show("该导游已存在");
                    }
                }
                else
                {
                    MessageBox.Show("身份证号输入有误");
                }
            }
            else
            {
                MessageBox.Show("手机号输入有误");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                this.pictureBox1.ImageLocation = openFileDialog1.FileName;
            }
        }
    }
}
